package ;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.Lib;
import format.png.Data;
import format.png.Tools;
import format.png.Writer;
import haxe.io.Bytes;
import haxe.io.BytesOutput;
import haxe.Log;
import haxe.Serializer;
import me.cunity.debug.Out;
import me.cunity.graphics.DrawTools;
import org.svgweb.core.SVGNode;
import org.svgweb.SVGLayout;
import me.cunity.graphics.DrawTools;
using me.cunity.graphics.DrawTools;


/**
 * ...
 * @author Axel Huizinga - axel@cunity.me
 */

class Main 
{
	static var svgLoader:SVGLayout;
	static var paramsObj:Dynamic;
	
	static function main() 
	{
		Out.traceTarget = CONSOLE;
		Log.trace = Out._trace;
		//Out.traceTarget = NATIVE;
		trace('hello world');
		
		svgLoader = new SVGLayout(layout);
		//svgLoader.setSize(100, 100);
		svgLoader.setSize(Lib.current.stage.stageWidth, Lib.current.stage.stageHeight);
		Lib.current.addChild(svgLoader);
		paramsObj = Lib.current.getChildAt(0).loaderInfo.parameters;
		Lib.current.graphics.clear();
		switch(paramsObj.bG)
		{
			case null:
			Lib.current.graphics.beginFill(0x5a5fa3);
			case 'transparent':
			//no bg
			default:
			Lib.current.graphics.beginFill(Std.parseInt('0x' + paramsObj.bG));
		}
		//Lib.current.graphics.drawRect(0, 0, Lib.current.stage.stageWidth, Lib.current.stage.stageHeight);		
		Lib.current.graphics.drawRect(0, 0, 100, 100);		

		if (false)
		{
			Lib.current.graphics.beginFill(0x5a5fa3);
			Lib.current.graphics.drawRect(0, 0, Lib.current.stage.stageWidth, Lib.current.stage.stageHeight);
		}
		
		trace(Std.string(paramsObj));
		trace(paramsObj.svgURL);
		if(paramsObj.svgURL != null)
			svgLoader.loadURL(paramsObj.svgURL);
	}
	
	static function layout()
	{
		var svgBox: Rectangle = null;
		var svgNode:SVGNode = svgLoader.svgRoot.getNode(paramsObj.node);
		if (svgNode == null)
		{
			trace('oops ' + paramsObj.node + ' not found :-(' );
			return;
		}		
		if (svgNode.clipSprite.mask != null)
		{
			//Out.dumpLayout(svgNode.clipSprite.mask);
			svgBox = svgNode.clipSprite.mask.getBounds(svgLoader.svgRoot.topSprite);
			//trace( svgNode.clipSprite.mask.getRect(svgLoader.svgRoot.topSprite));
			trace('clipSprite.mask.getBounds:' + svgBox);
		}
		else
			svgBox = svgNode.topSprite.getVisibleBounds();
		trace(svgBox);
		//var vBox:Rectangle = svgNode.topSprite.getVisibleBounds(svgBox);
		var vBox:Rectangle = svgNode.topSprite.getVisibleBounds();
		trace('getVisibleBounds:' + vBox);
		//vBox.offset(svgBox.x, svgBox.y);
		//vBox.offset(-svgBox.x, -svgBox.y);
		trace('childAt(0):' + svgNode.topSprite.getChildAt(0).getBounds(svgNode.topSprite));
		Out.dumpLayout(svgNode.topSprite);		
		var offset:Float = 0;
		//svgNode.topSprite.x = -svgBox.x + offset;
		//svgNode.topSprite.y = -svgBox.y + offset;
		/*svgNode.topSprite.x = -vBox.x + offset;
		svgNode.topSprite.x = -3;
		svgNode.topSprite.y = -vBox.y + offset;*/
		Lib.current.graphics.clear();
		//Lib.current.graphics.lineStyle(0,0xff00aa);
		Lib.current.graphics.beginFill(0x000060, 0.2);
		//Lib.current.graphics.drawRect(offset, offset, svgBox.width, svgBox.height);
		Lib.current.graphics.drawRect(vBox.x, vBox.y, vBox.width, vBox.height);
		//var svgDob:DisplayObject = Lib.current.addChild(svgNode.topSprite);
		//Lib.current.removeChild(Lib.current.getChildAt(0));
		Out.dumpLayout(svgNode.topSprite);
		trace(svgNode.clipSprite.mask.localToGlobal(new Point(0, 0)));
		//trace(svgDob.localToGlobal(new Point(0, 0)));

	}
	
	static function writePng(dO:DisplayObject)
	{
		var bytes:BytesBox = DrawTools.getData(dO);
		trace(bytes.bounds);
		var output:BytesOutput = new BytesOutput();
		output.bigEndian = true;
		var data:Data = Tools.build32BE(Math.floor(bytes.bounds.width), Math.floor(bytes.bounds.height), bytes.data);
		var writer:Writer = new Writer(output);
		writer.write(data);
		output.flush();
		//trace(output.getBytes().toString());
		trace(output.getBytes().length);		
	}
	
}